<!--   
Persistent Digital Archives and Library System (PeDALS) 
(c) 2008 - All Rights Reserved
http://blogs.msdn.com/sqlphp/archive/2009/03/23/paging-data-with-the-sql-server-driver-for-php.aspx

//-->
<?php
include 'includes/config.php'; 
$acqId = urldecode(@$_GET["acq"]);
$itemPull =  urldecode(@$_GET["pull"]);
/* query string not correct */
if ($acqId == "") {
	echo "Acquisition not found.  You did not select a valid acquisition for viewing.";
	die;
}
if (isset($_POST['Accept_Reject'])){	
	$accrej = $_POST['Accept_Reject'];
	$tsqlIngest = "{call PAC_UpdateReadyToIngest( ?, ?)}"; 
		$paramsIngest = array(   
		 array($acqId, SQLSRV_PARAM_IN),
		 array($accrej, SQLSRV_PARAM_IN)
	   ); 
	   $stmtIngest = sqlsrv_query( $conn, $tsqlIngest, $paramsIngest);	
	   $stmtIngest;   	
	   $_POST['Accept_Reject'] = "".$accrej."";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta name="description" content="Persistent Digital Archives and Library System" />
    <title>PeDALS Web Administration: Item Review</title>
    <link rel="shortcut icon" href="images/favicon.ico" />
    <link href="includes/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php include 'includes\header.php'; ?>
<div id="breadcrumb-container">
  <div id="breadcrumb"><a href="index.php" title=Home page">Home</a> / <a href="acqSummary.php" title="View Acquisitions">Acquisitions</a> / Items</div>
  <div id="breadcrumb-side"><form name="search" action="search.php"><input type="text" name="q" width="50"/><input type="submit" value="Go" /></form><a href="advSearch.php" title="Advanced Search" alt="Advanced Search">Advanced Search</a>
  </div>
  <div class="clear"></div>
</div>
<!-- 
  CONTENT
  -->
<div id="content-container">
  <!-- 
	SIDE COLUMN
	-->
  <div id="content-side">
    <ul class="link-list-vertical">
    <?php
$params = array(&$acqId);	
$tsql = "SELECT Provenance.ProvenanceName, Series.SeriesTitle, CONVERT(VARCHAR, SeriesAcquisition.SeriesAcquisitionIngestDate, 101) AS SeriesAcquisitionIngestDate, Provenance.ProvenanceId, Series.SeriesId FROM Provenance INNER JOIN Series ON Provenance.ProvenanceId = Series.ProvenanceId INNER JOIN SeriesAcquisition ON Series.SeriesId = SeriesAcquisition.SeriesId WHERE SeriesAcquisition.SeriesAcquisitionId = (?)";
		
	$getData = sqlsrv_prepare( $conn, $tsql, $params);
 	if( $getData === false ) {
		 echo "Error in preparing statement.\n";
		 die( print_r( sqlsrv_errors(), true));
	}
	/* Execute the statement. Display any errors that occur. */
	if( sqlsrv_execute( $getData))
	{   
	/*echo "Statement executed.\n";*/
	}
	else
	{
		 echo "Error in executing statement.\n";
		 die( print_r( sqlsrv_errors(), true));
	}
	
	$rowCount = 0;
 	while (sqlsrv_fetch( $getData ))
	{	
		$provName = sqlsrv_get_field($getData, 0);
		$seriestitle = sqlsrv_get_field($getData, 1);
		$ingestdate = sqlsrv_get_field($getData, 2);	
		$provid = sqlsrv_get_field($getData, 3);
		$serid = sqlsrv_get_field($getData, 4);
		$rowCount++;
	}
    ?>    
       <?php echo "<li><a title=\"View Provenance\"";
		echo " alt=\"View Provenance\"";
		echo " href=provDetail.php?prov=".$provid."";
		echo ">View Provenance</a></li>";		
		?>
       <?php echo "<li><a title=\"View Series\"";
		echo " alt=\"View Series\"";
		echo " href=serDetail.php?ser=".$serid."";
		echo ">View Series</a></li>";		
		?>
      <?php echo "<li><a title=\"View items\"";
		echo " alt=\"View items\"";
		echo " href=acqDetail.php?acq=".$acqId."";
		echo ">View Acquisition</a></li>";		
		?>
    </ul>
  </div>
  <!-- 
	MAIN COLUMN
	-->
  <div id="content"><a name="maincontent" id="maincontent"></a>
 
		<h1>Item Level Review</h1> 
        <h2><strong>Provenance:</strong> <?php echo $provName; ?><br/>
    	<strong>Series:</strong> <?php echo $seriestitle; ?></h2>
<?php
$tsqlIngest = "SELECT SeriesAcquisitionReadyToIngest FROM SeriesAcquisition WHERE SeriesAcquisitionId = (?)";
	$getData = sqlsrv_prepare( $conn, $tsqlIngest, $params);
 	if( $getData === false ) {
		 echo "Error in preparing statement.\n";
		 die( print_r( sqlsrv_errors(), true));
	}
	/* Execute the statement. Display any errors that occur. */
	if( sqlsrv_execute( $getData))
	{   
	/*echo "Statement executed.\n";*/
	}
	else
	{
		 echo "Error in executing statement.\n";
		 die( print_r( sqlsrv_errors(), true));
	}
	
 	while (sqlsrv_fetch( $getData ))
	{	
		$acqIngest = sqlsrv_get_field($getData, 0);
	}
	 
	switch ($acqIngest) {
    case 0: 
		echo "<h2>Waiting for submission.</h2>";
		break;
	case 1:
		echo "<h2>Acquisition submitted to PeDALS on ".$ingestdate." for processing. <br /> STATUS: Accepted.  BizTalk creating superpackage.</h2>";	
        break;
    case 2:
		echo "<h2>Acquisition submitted to PeDALS on ".$ingestdate." for processing. <br /> STATUS: Rejected.  BizTalk removing records.</h2>";	
        break;
    case 3:
		echo "<h2>Acquisition submitted to PeDALS on ".$ingestdate." for processing. <br /> STATUS: Rejected at least once.  Waiting for resubmission.</h2>";
        break;		
    case 4:
        echo "<h2>Ready for ingest by LOCKSS.  Superpackage has been created.</h2>";
        break;
    case 5:
		echo "<h2>Acquisition submitted to PeDALS on ".$ingestdate." for processing. <br /> STATUS: Ready for review.</h2>";
		echo "<form method=\"post\"><center>";
		echo "<input type=\"submit\" style=\"background-color:lightgrey\" value=\"Reject, delete acquisition\" onClick=\"document.getElementById('Accept_Reject').value='2';\"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		echo "<input type=\"submit\" style=\"background-color:lightgreen\" value=\"Accept, ingest acquisition\" onClick=\"document.getElementById('Accept_Reject').value='1';\"/>";
		echo "<input type=\"hidden\" name=\"Accept_Reject\" id=\"Accept_Reject\" value=\"\">";
		echo "</center></form>";
        break;
    case 9: 
		echo "<h2>BizTalk processing submitted records.</h2>";
		break;		
}

	$ItemCount = 0;
	$tsql = "{call PAC_GetItemCount(?, ?)}";  
	$params = array(   
				 array($acqId, SQLSRV_PARAM_IN),  
				 array($ItemCount, SQLSRV_PARAM_OUT)   
						   );  					   

	$stmt = sqlsrv_query( $conn, $tsql, $params);

	if ($itemPull == "rnd") {	
		echo "<p>".$ItemCount." total items in this acquisition. 50 shown here in random order.<br/>";
		echo "<a title=\"Review all items\"";
		echo " alt=\"Review all items\"";
		echo " href=itemSummary.php?acq=".$acqId."";
		echo ">Return All Items</a>&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;";		  
		echo "<a title=\"Return 50 randomly selected items\"";
		echo " alt=\"Return 50 randomly selected items\"";
		echo " href=itemSummary.php?pull=rnd&acq=".$acqId."";
		echo ">Refresh Random Set of 50</a>";
		echo "</p>";	
	} else {
		echo "<p>".$ItemCount." total items in this acquisition. All shown here sorted by title, 50 per page.<br/>";
		if ($ItemCount >= 50) { 
		echo "<a title=\"Return 50 randomly selected items\"";
		echo " alt=\"Return 50 randomly selected items\"";
		echo " href=itemSummary.php?pull=rnd&acq=".$acqId."";
		echo ">Return Random Set of 50</a>";
		}
		echo "</p>";	
	}
	
	$rowsPerPage = 50;
  
	if ($itemPull == "rnd") {	
					 
	/* Create the statement */
		$params = array(&$acqId);
		$tsql = "SELECT TOP (50) Item.ItemId, Item.ItemTitle, Item.ItemOriginatorIdentifier FROM SeriesAcquisitionItem_LINK
		INNER JOIN Item ON SeriesAcquisitionItem_LINK.ItemId = Item.ItemId
		WHERE SeriesAcquisitionItem_LINK.SeriesAcquisitionId = (?)
		ORDER BY NEWID()";
		  
 	/* Create the statement */
	$getData = sqlsrv_prepare($conn, $tsql, $params);
	if( $getData === false ) {
		 echo "Error in preparing statement.  Please contact your PeDALS Administrator.";
		 die;
	}

	/* Execute the statement */
	if( sqlsrv_execute( $getData))
	{ 	
	} else {
		 echo "Error in executing statement.   Please contact your PeDALS Administrator.";
		 die;
	}
    echo "<table class=\"table\" border=\"1\" cellspacing=\"0\" summary=\"Table listing the items for a particular acquisition\">";
	echo "<thead><tr>";
	echo "<th scope=\"col\">Item Id</th>";
	echo "<th scope=\"col\">Title</th>";
	echo "<th scope=\"col\">View</th>";
    echo "</tr></thead><tbody>";

	/* Start counting rows in the result set */
	$rowCount = 0;
	
	while (sqlsrv_fetch( $getData ))
	{
		$itemid = sqlsrv_get_field($getData, 0);
		$title = sqlsrv_get_field($getData, 1);
		$orginatorid = sqlsrv_get_field($getData, 2);
		
		echo "<tr>";
		echo "<td align=\"left\" valign=\"top\" >".$itemid."</td>";		
		echo "<td align=\"left\" valign=\"top\" >";
		echo "<a title=\"".$title."\"";
		echo " alt=\"".$title."\"";
		echo " href=itemDetail.php?id=".$itemid."";
		echo ">".$title."</a></td>";
		echo "<td align=\"center\" valign=\"top\" >";
		echo "<a title=\"View\"";
		echo " alt=\"View\"";
		echo " href=itemDetail.php?id=".$itemid."";
		echo "><img src='images/view.png' border=0/></a></td>";
		echo "</tr>";	
			
		$rowCount++;
    }	
	
	if( $rowCount == 0 ) { 
	    echo "<tr><td colspan=\"4\"><h4>There are no items to review yet for this acquisition.</h4></td></tr>"; 
	}
	
	echo "</tbody></table>";
	
    
				 
		 	} else {
	/* Pull all records */
	$tsql = "WITH Items AS
            (SELECT ROW_NUMBER() OVER(ORDER BY Item.ItemTitle) AS
                  RowNumber,
                  Item.ItemId,
                  Item.ItemTitle,
				  Item.ItemOriginatorIdentifier 
       FROM SeriesAcquisitionItem_LINK INNER JOIN Item ON SeriesAcquisitionItem_LINK.ItemId = Item.ItemId
	   WHERE SeriesAcquisitionItem_LINK.SeriesAcquisitionId = $acqId)
         SELECT * FROM Items
         WHERE RowNumber BETWEEN ? AND ? + 1";
	
/* Determine which row numbers to display. */
if(isset($_REQUEST['lowRowNum']) &&
   isset($_REQUEST['highRowNum']))
{
      $lowRowNum = $_REQUEST['lowRowNum'];
      $highRowNum = $_REQUEST['highRowNum'];
}
else
{
      $lowRowNum = 1;
      $highRowNum = $rowsPerPage;
}


/* Set query parameter values. */
$params = array(&$lowRowNum, &$highRowNum);

/* Execute the query. */
$stmt = sqlsrv_query($conn, $tsql, $params); 


/* Retrieve one row to see if there is any data. */
$row = sqlsrv_fetch_array($stmt);
if($row === false)
{
      echo "Error in fetching row.";
     die;
}
elseif($row[0] == 0) /* Special case of no data returned. */
{
      echo "No data returned.";
}
else /* A row was retrieved! */
{
   /* Set the number of rows that have been retrieved. */
   $rowsRetrieved = 1;
   
      if($lowRowNum > 1)
   {
      $prev_page_high = $lowRowNum;
      $prev_page_low = $lowRowNum - $rowsPerPage;
      $prevPage = "?acq=$acqId&lowRowNum=$prev_page_low".
                  "&highRowNum=$prev_page_high";
      echo"<a href=$prevPage>".
            "Previous Page</a>&nbsp;&nbsp;&nbsp;";
   }
   /* If there are more results, display the Next Page link.
      We know there are more results if the last call to
      sqlsrv_fetch_array returned a row (the "extra" row).
   */

   if($row != false)
   {       
      $next_page_low = $highRowNum;
      $next_page_high = $highRowNum + $rowsPerPage;
      $nextPage = "?acq=$acqId&lowRowNum=$next_page_low".
                  "&highRowNum=$next_page_high";
      echo "<a href=$nextPage>Next Page</a>";
   }    

   /*Display table header. */
   echo "<br/><table class=\"table\" border=\"1\" cellspacing=\"0\" summary=\"Table listing the items for a particular acquisition\">";
	echo "<thead><tr>";
	echo "<th scope=\"col\">Row</th>";
	echo "<th scope=\"col\">Item Id</th>";
	echo "<th scope=\"col\">Title</th>";
	echo "<th scope=\"col\">View</th>";
    echo "</tr></thead><tbody>";

   /* Display the retrieved rows while we haven't
      displayed all of $rowsPerPage and there is
      another row to display. */
   do
   {
		echo "<tr>";
		echo "<td align=\"left\" valign=\"top\" >".$row[0]."</td>";	
		echo "<td align=\"left\" valign=\"top\" >".$row[1]."</td>";		
		echo "<td align=\"left\" valign=\"top\" >";
		echo "<a title=\"".$row[2]."\"";
		echo " alt=\"".$row[2]."\"";
		echo " href=itemDetail.php?id=".$row[1]."";
		echo ">".$row[2]."</a></td>";
		echo "<td align=\"center\" valign=\"top\" >";
		echo "<a title=\"View\"";
		echo " alt=\"View\"";
		echo " href=itemDetail.php?id=".$row[1]."";
		echo "><img src='images/view.png' border=0/></a></td>";
		echo "</tr>";	
         $rowsRetrieved++;
   } while ($rowsRetrieved <= $rowsPerPage &
            $row = sqlsrv_fetch_array($stmt));
   /* Close table. */
   echo "</table>";
   /*If there are previous results, display the
     Previous Page link.*/
   if($lowRowNum > 1)
   {
      $prev_page_high = $lowRowNum;
      $prev_page_low = $lowRowNum - $rowsPerPage;
      $prevPage = "?acq=$acqId&lowRowNum=$prev_page_low".
                  "&highRowNum=$prev_page_high";
      echo"<a href=$prevPage>".
            "Previous Page</a>&nbsp;&nbsp;&nbsp;";
   }
   /* If there are more results, display the Next Page link.
      We know there are more results if the last call to
      sqlsrv_fetch_array returned a row (the "extra" row).
   */

   if($row != false)
   {       
      $next_page_low = $highRowNum;
      $next_page_high = $highRowNum + $rowsPerPage;
      $nextPage = "?acq=$acqId&lowRowNum=$next_page_low".
                  "&highRowNum=$next_page_high";
      echo "<a href=$nextPage>Next Page</a>";
   }    
} 
}	
	sqlsrv_close( $conn);
	include 'includes\footer.php'; ?>
  </div>
</div>
</body>
</html>